package com.warmheart.impl.sys;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.warmheart.base.config.WhTransactionalControl;
import com.warmheart.base.page.BasePage;
import com.warmheart.base.page.PageInfo;
import com.warmheart.base.page.Pager;
import com.warmheart.base.service.impl.BaseService;
import com.warmheart.core.util.EntityCommonCopyUtil;
import com.warmheart.dao.sys.SysDictBaseDao;
import com.warmheart.entity.sys.SysDictBase;
import com.warmheart.model.DictCache;
import com.warmheart.service.sys.ISysDictBaseService;

/**
 * 
 * @ClassName: SysDictBaseService
 * @Description: 数据字典Service，对应表名称为tb_sys_dict_base
 * 温馨提示：对外提供的接口，原则上这里只写自定义方法
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@Service(value = "sysDictBaseService")
public class SysDictBaseService extends BaseService<SysDictBase> implements ISysDictBaseService {

    @Autowired
    private SysDictBaseDao sysDictBaseDao;

    @Override
    public SysDictBaseDao getDao() {
        return this.sysDictBaseDao;
    }
    
    @Override
    public SysDictBase getSysDictBaseLabel(SysDictBase dictBase) {
        return getDao().selectOneCustom(dictBase, "getSysDictBaseLabel");
    }

    @Override
    public SysDictBase getDictName(SysDictBase dictBase) {
        return getDao().selectOneCustom(dictBase, "getDictName");
    }

    @Override
    public List<SysDictBase> getSysDictBaseList(Object dictType) {
        return getDao().selectListCustom(dictType, "getSysDictBaseList");
    }

    /** 获取字典类型的列表 */
    @Override
    public PageInfo<SysDictBase> queryTypeByList(BasePage page) throws Exception {
        Integer rowCount = getDao().selectOneCustom(page, "queryTypeByCount");
        Pager pager = page.getPager();
        pager.setRowCount(rowCount);
        List<SysDictBase> dataList = getDao().selectListCustom(page, "queryTypeByList");
        return new PageInfo<SysDictBase>(dataList, pager);
    }

    /**
     * 字典类型添加与修改
     *
     * @param dictBase
     * @throws Exception
     */
    @Override
    public void addTypeSave(SysDictBase dictBase) throws Exception {
        // 新增保存
        this.getDao().add(dictBase);
    }

    /**
     * 字典类型添加与修改
     *
     * @param dictBase
     * @throws Exception
     */
    @Override
    @WhTransactionalControl
    public void updateTypeSave(SysDictBase dictBase, String oldDictType) throws Exception {
        // 更新保存
        List<SysDictBase> dictList = getSysDictBaseList(oldDictType);

        for (SysDictBase dict : dictList) {
            dict.setDictType(dictBase.getDictType());
            dict.setDictTypeName(dictBase.getDictTypeName());
            EntityCommonCopyUtil.updateOld2UpdateNew(dictBase, dict);
            this.getDao().updateBySelective(dict);
        }
    }

    /**
     * 字典类型删除
     *
     * @param dictBase
     * @throws Exception
     */
    @Override
    @WhTransactionalControl
    public void deleteDictType(SysDictBase dictBase) throws Exception {

        // 物理删除
        if (dictBase == null || StringUtils.isBlank(dictBase.getDictType())) {
            return;
        }
        List<SysDictBase> dictList = getSysDictBaseList(dictBase.getDictType());
        // 更新保存
        for (SysDictBase dict : dictList) {
            getDao().deleteById(dict.getId());
        }
    }

    /**
     * 验证字典是否存在
     *
     * @param dictBase
     * @return
     */
    @Override
    public List<SysDictBase> checkSysDictBaseExist(SysDictBase dictBase) {
        return getDao().selectListCustom(dictBase, "checkSysDictBaseExist");
    }

    /**
     * 根据Type获取到字典类型
     *
     * @param dictBase
     * @return
     */
    @Override
    public SysDictBase queryByType(SysDictBase dictBase) {
        return getDao().selectOneCustom(dictBase, "queryByType");
    }

    /**
     * 获取多个字典值
     *
     * @param dictBase
     * @return
     */
    @Override
    public List<SysDictBase> getSysDictBaseLabels(SysDictBase dictBase) {
        return getDao().selectListCustom(dictBase, "getSysDictBaseLabels");
    }

    /**
     * 获取所有的字典值，用于缓存
     *
     * @return
     */
    @Override
    public List<DictCache> getDictForCaches() {
        return getDao().selectListCustom(null, "getDictForCaches");
    }
}